import ExtendedFrame from '../demo/extended-frame.js';
class Workspace{
    constructor(elem, configs){
        this.elem = elem;
        this.children = [];
        this.list = [];
        this.configs = Object.assign({}, Workspace.defaultConfigs, configs);
    }
    _add(child){
        child.zIndex = this.children.length;
        this.children.push(child);
        this.list.push(child);
        this.elem.append(child.elem);
        if(this.configs.onChange){
            this.configs.onChange([...this.list]);
        }
        return child;
    }
    rand(min, max){
        return Math.floor(Math.random()*(max-min)+min);
    }
    front(child){
        var index = this.children.indexOf(child);
        if(index >= 0){
            if(index < (this.children.length - 1)){
                this.children.splice(index, 1);
                this.children.push(child);
                this.children.forEach(function(frame, i){
                    frame.zIndex = i;
                });
            }
        }
        return this;
    }
    remove(child){
        var index = this.children.indexOf(child);
        var i = this.list.indexOf(child);
        if(index >= 0){
            child._del();
            if(index < (this.children.length - 1)){
                this.children.splice(index, 1);
                this.children.forEach(function(frame, i){
                    frame.zIndex = i;
                });
            }else{
                this.children.pop();
            }
            if(i > 0) this.list.splice(i, 1);
        }
        if(this.configs.onChange){
            this.configs.onChange([...this.list]);
        }
        return this;
    }
    addFrame(configs){
        var tconfigs = {};
        if(!configs) tconfigs = {};
        else tconfigs = configs;
        var frame = new ExtendedFrame(
            tconfigs.x || this.configs.x || this.rand(0, this.elem.offsetWidth / 2),
            tconfigs.y || this.configs.y || this.rand(0, this.elem.offsetHeight / 2),
            tconfigs.width || this.configs.width || this.rand(this.elem.offsetWidth / 3, this.elem.offsetWidth / 2),
            tconfigs.height || this.configs.height || this.rand(this.elem.offsetHeight / 3, this.elem.offsetHeight / 2),
            this,
            this.configs,
            tconfigs
        );
        return frame;
    }
}

Workspace.defaultConfigs = {
    className: "fui",
    title: "未命名",
    resizeBorderWidth: 16,
    minWidth: 280,
    minHeight: 30,
    icon: './static/images/12.png',
    onChange: (list)=>console.log(list)
};
export default Workspace;